package com.dalianbank.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

//import org.apache.commons.dbcp.*;

public class DBTools {
	/*
	private static BasicDataSource datasource;
	private static BasicDataSource as400source;
	
	static {
		resetDBPool();
	}
	
	public static void resetDBPool(){
		if(datasource != null)
		{
			try {
				datasource.close();
			} catch (SQLException e) {
				ParamCfg.log.error(e.getMessage(),e);
			}
		}
		datasource=new BasicDataSource();
		datasource.setDriverClassName(ParamCfg.db2Driver);
		datasource.setUrl(ParamCfg.db2Url);
		datasource.setUsername(ParamCfg.db2User);
		datasource.setPassword(ParamCfg.db2Pwd);
		
		datasource.setInitialSize(ParamCfg.initSize);
		datasource.setMaxIdle(ParamCfg.maxIdle); 
		datasource.setMaxActive(ParamCfg.maxActive);
		datasource.setMaxWait(ParamCfg.maxWait);
		datasource.setTimeBetweenEvictionRunsMillis(ParamCfg.timeBetweenEvictionRunsMillis);
		datasource.setMinIdle(ParamCfg.minIdle);
		datasource.setMinEvictableIdleTimeMillis(ParamCfg.minEvictableIdleTimeMillis);
		
		as400source=new BasicDataSource();
		as400source.setDriverClassName(ParamCfg.as400Driver);
		as400source.setUrl(ParamCfg.as400Url);
		as400source.setUsername(ParamCfg.as400User);
		as400source.setPassword(ParamCfg.as400Pwd);
		
		as400source.setInitialSize(0);
		as400source.setMaxIdle(ParamCfg.maxActive);
		as400source.setMaxActive(ParamCfg.maxActive);
		as400source.setMaxWait(ParamCfg.maxWait);
		as400source.setTimeBetweenEvictionRunsMillis(ParamCfg.timeBetweenEvictionRunsMillis);
		as400source.setMinIdle(ParamCfg.minIdle);
		as400source.setMinEvictableIdleTimeMillis(ParamCfg.minEvictableIdleTimeMillis);
	}
	
	public static void shutdown() throws SQLException
	{
		datasource.close();
		as400source.close();
	}
	*/
	
	public  static Connection crtConn(String driver, String dburl, String user, String pwd) throws Exception{
		Connection con;
		Properties props = new Properties();
		Class.forName(driver);
		props.put("user", user);
		props.put("password", pwd);
		//props.put("naming", "system");
		//props.put("libraries", "," + ParamCfg.as400LibList);
		con = DriverManager.getConnection(dburl, props);
		con.setAutoCommit(false);
		con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		return con;
	}
	
	/*
	public static Connection getConnection() throws SQLException
	{	
		return datasource.getConnection();
	}
	
	public static Connection getAs400Connection() throws SQLException
	{	
		return as400source.getConnection();
	}
	*/
}
